package microservice

import (
	"fmt"

	"github.com/vifraa/gopom"
)

// #region JAVA
const docker_template_java = `
FROM openjdk:8-jre

RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

ADD ./target/%s /app/
CMD ["java", "-Xmx200m",  "-jar", "/app/%s"]

EXPOSE %d
`

//#endregion

// #region PYTHON
const Docker_Python_template = `FROM python:3
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

RUN apt update -y
RUN apt install -y cmake
RUN apt install -y libgl1-mesa-glx

ENV PYTHONUNBUFFERED=TRUE

RUN mkdir -p /app
ADD . /app/
WORKDIR /app

RUN pip install -r requirements.txt

CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:17001", "app:app"]

EXPOSE 17001
`

//#endregion

// #region JS_NODE_template
const JS_NODE_template = `FROM node
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

RUN mkdir -p /app
WORKDIR /app

ADD ./node_modules /app/node_modules
ADD ./package.json /app/
ADD ./public /app/public
# RUN npm install

ADD ./bin /app/bin
ADD ./app.js /app/
CMD [ "npm", "start" ]

EXPOSE 16108
`

//#endregion

// #region  OpenResty_template
const OpenResty_template = `FROM openresty/openresty:trusty

RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo 'Asia/Shanghai' >/etc/timezone

COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf

COPY static /usr/share/nginx/html
`

//#endregion

// #region GO
const GO_DOCKER = `FROM mrrm/web.go

RUN mkdir -p /app
WORKDIR /app

ADD ./src/main/main.go /app/
RUN go build
CMD [ "./app" ]

EXPOSE 12862
`

// #endregion
type template_java struct {
	Jarname string
	Port    uint
}

func GenerateJavaTemplate(path string) (string, error) {
	//path string
	var template template_java
	yamlconfig, pomconfig := parseSubmodule(path)
	template.Jarname = GenerateJarPath(pomconfig)
	template.Port = yamlconfig.Serve.Port
	if len(template.Jarname) < 2 {
		template.Jarname = pomconfig.ArtifactID + "-1.0-SNAPSHOT.jar"
	}
	return fmt.Sprintf(docker_template_java, template.Jarname, template.Jarname, template.Port), nil

}
func autoGenDockerfile(path string) (lt LanguageType, genfile string) {
	_, lt = checkSubModule(path)
	var err error
	switch lt {
	case _UNDEFINE_:
		genfile = ""
	case _JAVA_:
		genfile, err = GenerateJavaTemplate(path)
	case _PYTHON_:
		genfile = Docker_Python_template
	case _NODEJS_:
		genfile = JS_NODE_template
	case _GO_:
		genfile = GO_DOCKER
	case _RUST_:
		genfile = "UNSUPPORT LANGUAGE TYEP IN Docfile Gen"
	}
	if err != nil {
		return _UNDEFINE_, "err in generation "
	}
	genfile += "\n #The above code  is auto generated by the G8s Service , please change it if you need\n"
	return
}

func parseSubmodule(path string) (yaml ConfigYaml, pom gopom.Project) {
	yamlpath, exist := assertFindYaml(path)
	if exist {
		yaml, _ = ParseYamlFile(yamlpath)
	}

	pompath, exist := assertFindPom(path)
	if exist {
		pom, _ = ParsePomFile(pompath)

	}
	return
}
